package com.itheima.studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {

    private static final String ADD_STUDENT="1";
    private static final String DELETE_STUDENT="2";
    private static final String UPDATE_STUDENT="3";
    private static final String QUERY_STUDENT="4";
    private static final String EXIT="5";

    public static void mai() {

        //创建一个集合
        ArrayList<Student> list = new ArrayList<>();

        //创建一些学生对象并存入集合
        Student s1 = new Student("001", "zhangsan", 18, "beijing");
        list.add(s1);
        Student s2 = new Student("002", "lisi", 19, "shanghai");
        list.add(s2);

        loop:
        while (true) {
            System.out.println("---------欢迎来到学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择：");

            Scanner sc = new Scanner(System.in);
            String choice = sc.next();
            switch (choice) {
                case ADD_STUDENT -> addStudent(list);
                case DELETE_STUDENT -> deleteStudent(list);
                case UPDATE_STUDENT -> updateStudent(list);
                case QUERY_STUDENT -> queryStudent(list);
                case EXIT -> {
                    System.out.println("退出");
                    break loop;
                    //System.exit(0);
                }
                default -> System.out.println("输入错误，请重新输入");
            }
        }


    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Student stu = new Student();

        Scanner sc = new Scanner(System.in);

        //输入信息
        System.out.println("请输入学生id");
        String id = sc.next();
        stu.setId(id);
        System.out.println("请输入学生姓名");
        String name = sc.next();
        stu.setName(name);
        System.out.println("请输入学生年龄");
        int score = sc.nextInt();
        stu.setAge(score);
        System.out.println("请输入学生地址");
        String address = sc.next();
        stu.setAddress(address);

        //判断id是否唯一
        if (contains(list, id)) {
            //id已经存在
            System.out.println("id已经存在,请重新输入");
        } else {
            //id不存在,可以添加
            list.add(stu);
            //到这里就已经添加完成了
            System.out.println("添加学生成功");
        }
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入要删除学生的id");
        //获取id
        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        //判断id是否存在
        if (contains(list, id)) {
            //存在，可以删除
            //需要获取该id学生对应的索引
            int index = getIndex(list, id);
            //删除
            list.remove(index);
            System.out.println("删除成功");
        } else {
            //不存在，提示一下
            System.out.println("id不存在");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list) {
        System.out.println("请输入要修改的学生id");

        //键盘录入
        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        //获取要修改id对应的索引
        int index = getIndex(list, id);

        //判断id是否存在
        if (index >= 0) {
            //存在，可以修改
            //开始修改
            System.out.println("请输入学生新id");
            String newId = sc.next();
            System.out.println("请输入学生姓名");
            String name = sc.next();
            System.out.println("请输入学生年龄");
            int age = sc.nextInt();
            System.out.println("请输入学生地址");
            String address = sc.next();

            //创建学生对象
            Student stu = new Student(newId, name, age, address);
            //修改集合中的元素
            list.set(index, stu);
        } else {
            //不存在，提示一下
            System.out.println("id不存在");
        }
    }

    //查询学生
    public static void queryStudent(ArrayList<Student> list) {
        //进来先判断一下集合里有没有学生、
        if (list.size() == 0) {
            System.out.println("无信息，请先添加信息再查询");
            return;
        }

        System.out.println("id\t姓名\t年龄\t地址");
        //遍历打印
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + "\t" + list.get(i).getName() + "\t\t"
                    + list.get(i).getAge() + "\t" + list.get(i).getAddress());
        }
    }

    //判断id是否存在
    public static boolean contains(ArrayList<Student> list, String id) {
        return getIndex(list, id) >= 0;
    }

    //获取id对应的索引
    public static int getIndex(ArrayList<Student> list, String id) {
        //先判断集合是否为空
        if (list.size() == 0) return -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                //说明找到了用户输入id对应的索引
                return i;
            }
        }
        //没有找到
        return -1;
    }
}
